package com.tiger.userClient.http.home;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apereo.cas.client.authentication.AttributePrincipal;
import org.apereo.cas.client.util.AbstractCasFilter;
import org.apereo.cas.client.validation.Assertion;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/home")
public class HomeController {
    @Value("${server.servlet.context-path}")
    private String contextPath;


    @Value("${APP_HOST}")
    private String APP_HOST;


//    @GetMapping
//    public void home(HttpServletRequest request) {
//        Map<String,Object> attributes = new HashMap<String, Object>();
//        Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
//        System.out.println(assertion);
//        if(assertion != null) {
//            Principal principal  = assertion.getPrincipal();
//            String loginName =principal.getName();
//            System.out.printf("登录用户名:%s\r\n",loginName);
//            System.out.printf("ValidFromDate:%s\r\n",assertion.getValidFromDate());
//            System.out.printf("ValidUntilDate:%s\r\n",assertion.getValidUntilDate());
//            System.out.printf("AuthenticationDate:%s\r\n",assertion.getAuthenticationDate());
//
//            //获取自定义返回值的数据
//            if (principal instanceof AttributePrincipal) {
//                //cas传递过来的数据
//                attributes = ((AttributePrincipal)principal).getAttributes();
//                for(Map.Entry<String, Object> entry : attributes.entrySet()) {
//                    String key = entry.getKey();
//                    Object val = entry.getValue();
//                    System.out.printf("**************%s:%s\r\n",key,val);
//                }
//            }
//        }
//    }


//    @GetMapping("a")
//    public void a() {
//        String mobileUrl = "https://oa.swun.edu.cn/seeyon/rest/jssdk/toSpace?type=mob&tourl=%2Fseeyon%2Fm3%2Fapps%2Fv5%2Fcollaboration%2Fhtml%2Fdetails%2Fsummary.html%3FaffairId%3D2924323934902374293%26openFrom%3DlistPending%26summaryId%3D-1%26proxyId%3D0%26VJoinOpen%3DVJoin";
//        String pcUrl = "https://oa.swun.edu.cn/seeyon/thirdpartyController.do?method=access&enc=TkpFPi4zNDk3MzM6Mjk4Njg0NDExNTU1J00%2BbmZ0dGJoZi9tam9sL2RwbS94YmpUZm9lJ1U%2BMjgzOjg2NTgxMzc6OCdOPjQxMTExMTExMTEzMTYyJ2RpZmRsZmU%2BdXN2Zg%3D%3D&isGroupThird=true&P=2924323934902374293&P=-764292068534170576";
//
//
//
//        String baseUrl = "http://localhost:10235";
//        if(!contextPath.isEmpty()) {
//            baseUrl += contextPath;
//        }
//
//        baseUrl += "/home/message";
//
//        try{
//            String url = baseUrl + ("?mobileUrl=" + URLEncoder.encode(mobileUrl, StandardCharsets.UTF_8)) + "&pcUrl=" + URLEncoder.encode(pcUrl, StandardCharsets.UTF_8);
//            System.out.println(url);
//
//        }catch (Exception e) {}
//    }

    @GetMapping("message")
    public void message(@RequestParam Map<String, String> query, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println(query);
        String userAgent = request.getHeader("User-Agent").toLowerCase();

        boolean isMobile = false;
        if (userAgent.contains("mobi") || userAgent.contains("android")) {
            isMobile = true;
        } else if (userAgent.contains("ipad") || userAgent.contains("tablet")) {
            isMobile = true;
        }

        String url = query.get(isMobile ? "mobileUrl" : "pcUrl");

        response.sendRedirect(url);
    }
}
